from datetime import datetime
import random
import string

from common.setting import ensure_path_sep
from util.mysqlUtils.db_pool import Database
from util.otherutils.functions_control import randomint


def generate_random_string(length):
    """生成指定长度的随机字符串"""
    letters = string.ascii_lowercase + string.digits
    return ''.join(random.choice(letters) for i in range(length))


def generate_random_ip():
    """生成随机的 IP 地址"""
    return '.'.join(str(random.randint(0, 255)) for _ in range(4))


def generate_random_mac():
    """生成随机的 MAC 地址"""
    return ':'.join('{:02x}'.format(random.randint(0, 255)) for _ in range(6))


d_cginstance_db = Database(ensure_path_sep("\\config\\mysql_config.yaml"), "sit_union", "d_cginstance")


class GenerageData:

    def __init__(self,arch, instance_type, spec, idc, system_image_id, ecn):
        self.arch = arch
        self.instance_type = instance_type
        self.spec = spec
        self.idc = idc
        self.system_image_id = system_image_id
        self.ecn = ecn

    def generage_data(self):
        # 生成随机的时间
        now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        # 生成随机的 ID
        g_instance_id = f'ins-{generate_random_string(32)}'
        g_server_id = f'hyp-{generate_random_string(20)}'
        # 随机生成其他字段的值
        project_id = 0
        status = 0
        supplier = 1
        supplier_instance_id = g_instance_id
        supplier_server_id = g_server_id
        supplier_order_id = str(random.randint(10000, 99999))
        supplier_business_status = 'InstanceNormal'
        ins_state = 'Running'
        task_state = 'None'
        reboot_at = None
        multi_count = 0
        remark = None
        current_sub_task_id = ''
        multi_open_arch = 0
        sql_instance = f"""
            INSERT INTO d_cginstance.t_instance 
            (created_at, updated_at, deleted_at, updated_by, g_instance_id, g_server_id, project_id, status, arch, 
            instance_type, spec, idc, supplier, supplier_instance_id, supplier_server_id, supplier_order_id, 
            supplier_business_status, system_image_id, ins_state, task_state, reboot_at, multi_count, 
            remark, ecn, current_sub_task_id, multi_open_arch) 
            VALUES (now(), now(), %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
            """
        values_instance = (
            None, 0, g_instance_id, g_server_id, project_id, status, self.arch,
            self.instance_type, self.spec, self.idc, supplier, supplier_instance_id, supplier_server_id, supplier_order_id,
            supplier_business_status, system_image_id, ins_state, task_state, reboot_at, multi_count,
            remark, self.ecn, current_sub_task_id, multi_open_arch
        )

        # 生成 t_instance_network 表的数据
        network_id = random.randint(10000, 99999)
        network_type = 'Main'
        ip_address = "10.0.0.1"
        mac_address = generate_random_mac()

        # SQL 插入 t_instance_network 表语句
        sql_network = """
        INSERT INTO d_cginstance.t_instance_network 
        (id, created_at, updated_at, deleted_at, `type`, ip_address, mac_address, g_instance_id)
        VALUES (%s, now(), now(), %s, %s, %s, %s, %s)
        """
        values_network = (
            network_id, None, network_type, ip_address, mac_address, g_instance_id
        )
        rows1 = d_cginstance_db.execute(sql_instance, values_instance)
        rows2 = d_cginstance_db.execute(sql_network, values_network)
    def generate_many_data(self,num):
        for i in range(num):
            self.generage_data()





if __name__ == '__main__':
    # 示例参数
    arch = 'X86'
    instance_type = 'BareMetalDiskLess'
    spec = 'egc-pcfi5-3060-v1-A'
    idc = 'cn-east-jinhua-3001'
    system_image_id = 'os-r47trzuv6vbkxve2jyi0xs9x4owb'
    ecn = '202'

    GenerageData(arch, instance_type, spec, idc, system_image_id, ecn).generate_many_data(6)
